<template>
  <el-dialog title="新增部门" :visible="showDialog" @close="closeFn">
    <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" size="small" @click="addUserFn">确定</el-button>
      <el-button size="small" @click="cancelFn">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 引入部门负责人数据
import { getEmployeeSimple } from '@/api/employees'
// 引入部门增添的API接口
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {

  props: {
    showDialog: {
      type: Boolean
    },
    addUserId: {
      type: Object,
      default() {
        return {}
      }
    },
    userCode: {
      type: Array,
      default() {
        return []
      }
    }
  },

  data() {
    const validateCode = (rule, value, callback) => {
      let list = []
      if (this.form.id) {
        // 则说明是编辑状态
        console.log(123)
        console.log(this.userCode)
        console.log(this.editCode)
        list = this.userCode.filter(item => item !== this.editCode)
      } else {
        list = this.userCode
      }
      if (list.includes(value)) {
        return callback(new Error('该编码已存在'))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      // 部门负责人数据
      userList: [],
      // 编辑时的Code码
      editCode: null
    }
  },
  created() {
    this.userData()
  },
  methods: {
    // 点击关闭
    closeFn() {
      this.$emit('close-dialog')
      // 清空输入框的内容
      this.$refs.deptForm.resetFields()
    },
    async userData() {
      const res = await getEmployeeSimple()
      console.log(res)
      this.userList = res
    },
    //  点击确认按钮，先进行兜底测验，成功之后发送请求
    addUserFn() {
      console.log(this.$refs.deptForm)
      this.$refs.deptForm.validate(async(valible) => {
        if (valible) {
          // console.log(this.addUserId)
          // 通过id判断到底是编辑还是新增
          if (this.form.id) {
            // id如果存在的话，那就是编辑修改
            await updateDepartments(this.form)
          } else {
            // 新增
            // 发送请求
            await addDepartments({ ...this.form, pid: this.addUserId.id })
          }

          // 数据获取成功之后，关闭弹窗
          this.$emit('close-dialog')
          // 提示添加成功
          this.$message.success('添加成功')
          // 自动更新
          this.$emit('updateData')
          // 清空输入框的内容
          this.$refs.deptForm.resetFields()
        }
      })
    },
    // 取消关闭弹窗
    cancelFn() {
      this.$emit('close-dialog')
      // 清空输入框的内容
      this.$refs.deptForm.resetFields()
    },
    // 获取编辑数据
    async getDepartmentData(id) {
      // console.log(this.addUserId.id)
      const res = await getDepartDetail(id)
      console.log(res)
      // 结构赋值，数据回填
      this.form = res
      // 将编辑时候的Code码保存到editCode中
      this.editCode = this.form.code
      console.log(this.editCode)
    },
    // 点击编辑按钮，弹窗弹出来的时候，发送请求
    editDepartData(id) {
      this.getDepartmentData(id)
    }
  }

}
</script>

<style>

</style>
